
const OpenAI = require('openai');
const express = require('express');
const router = express.Router();

require('dotenv').config();

const openai = new OpenAI({
    baseURL: process.env.DEEPSEEK_API_BASE_URL,
    apiKey: process.env.DEEPSEEK_API_KEY
});

/* GET users listing. */
router.get('/', function (req, res, next) {
    res.send('respond with ai');
});

/** 获取template的关键json */
router.post('/grtJson', async (req, res, next) => {

    const resultDto = {
        code: 200,
        data: null,
        message: "success"
    }

    const { desc } = req.body;

    // 构建DeepSeek API请求
    const prompt = `请${desc}。生成指定格式JSON数组：[
        {
            "icon": "Edit",
            "text": "Opro表格",
            "name": "OproTable",
            "props": {
            "modelLabel": "考试管理",
            "modelName": "exam-manage",
            "listApi": "",
            "detailApi": "",
            "delApi": "",
            "addApi": "",
            "editApi": "",
            "apiPath": "./service",
            "exportApi": ""
            },
            "category": "container",
            "subWidgets": [
            {
                "icon": "Edit",
                "text": "输入框",
                "name": "OproField",
                "props": {
                "fieldLabel": "姓名",
                "fieldName": "name"
                },
                "category": "filed",
            },
            {
                "icon": "Edit",
                "text": "输入框",
                "name": "OproField",
                "props": {
                "fieldLabel": "年龄",
                "fieldName": "age",
                "checked": true,
                "canSearch": false,
                "fieldType": "input",
                "charLength": 50
                },
                "category": "filed",
            }
            ],
        }
        ]。其中只有modelLabel、modelName、fieldLabel、fieldName4个属性的值可变，其他属性固定`;

    const completion = await openai.chat.completions.create({
        messages: [{ role: "system", content: prompt }],
        model: "deepseek-chat",
    });

    console.log("completion:xxxxx")
    resultDto.data = completion.choices[0].message.content;

    res.send(resultDto);
});



module.exports = router;
